package springMvc;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;

import entity.User;
@Controller//控制器Controller 负责处理由DispatcherServlet 分发的请求
public class SpringMvcLojin {
/*@RequestMapping("/hello")
public String login(String uname,int age){
	System.out.println("+++++++++++++++执行longgin登录方法+++++++++++++++++++++++++++"+uname+",age"+age);
	return "success";
}*/
	//通过实体类封装
	
/*	@RequestMapping("/hello")
	public String login(User user){
		System.out.println("+++++++++++++++执行longgin登录方法+++++++++++++++++++++++++++"+user.getUname()+",age"+user.getAge());
		return "success";
	}*/
	//通过原生方式
	/*@RequestMapping("/hello")
	public String login(HttpServletRequest request){
		System.out.println("+++++++++++++++执行longgin登录方法+++++++++++++++++++++++++++"+request.getParameter("uname"));
		ModelAndView ma=new ModelAndView("success");
		User user=new User();
		user.setUname("tom");
		user.setAge(22);
		ma.addObject("user",user);
		return "success";
	}*/
	@RequestMapping("/success")
	public ModelAndView login(@RequestParam("uname")String uname,@RequestParam("age")int age){
		System.out.println("+++++++++++++++执行longgin登录方法+++++++++++++++++++++++++++"+uname+",--"+age);
		ModelAndView ma=new ModelAndView("success");
		ma.addObject("uname","tom");
		User user=new User();
		user.setUname("tom");
		user.setAge(22);
		ma.addObject("user",user);
		List<User> list=new ArrayList<>();
		list.add(new User("tom",12));
		list.add(new User("jick",22));
		list.add(new User("lilli",32));
		ma.addObject("list",list);
		
		Map<Object,Object> map=new HashMap<>();
	   map.put("政治部", new User("张三",22));
	   map.put("财务部", new User("李四",22));
	   map.put("商业部", new User("王二",22));
	   ma.addObject("map",map);
	   
		return ma;
	}
	//不想去除属性用方式一//去除属性在属性上面加@JsonIgnore
	@RequestMapping("/ajax")
	@ResponseBody//自动转json注解
	public User Ajax(){
		System.out.println("+++++++++++++++执行Ajax方法+++++++++++++++++++++++++++");
		User user=new User();
		user.setUname("tom");
		user.setAge(22);
		return user;
}
	
	//想去除属性用方式二
	@RequestMapping("/ajax1")
	@ResponseBody//自动转json注解
	public String Ajax1(){
		System.out.println("+++++++++++++++执行Ajax1方法+++++++++++++++++++++++++++");
		User user=new User();
		user.setUname("tom");
		user.setAge(22);
		Gson gson=new Gson();
		String gsonStr=gson.toJson(user);
		return gsonStr;
}
	//list集合属性封装json
	@RequestMapping("/ajax2")
	@ResponseBody//自动转json注解
	public List<User> Ajax2(){
		System.out.println("+++++++++++++++执行Ajax2方法+++++++++++++++++++++++++++");
		List<User> list=new ArrayList<>();
		list.add(new User("tom",12));
		list.add(new User("jick",22));
		list.add(new User("lilli",32));
		return list;
}
	@RequestMapping("/ajax3")
	@ResponseBody//自动转json注解
	public String Ajax3(){
		System.out.println("+++++++++++++++执行Ajax3方法+++++++++++++++++++++++++++");
		List<User> list=new ArrayList<>();
		list.add(new User("tom",12));
		list.add(new User("jick",22));
		list.add(new User("lilli",32));
		Gson gson=new Gson();
		String gsonStr=gson.toJson(list);
		return gsonStr;
}
	
	//从定向转发请求
	@RequestMapping("/action1")
	public String action(RedirectAttributes ra){
		System.out.println("+++++++++++++++执行action1方法+++++++++++++++++++++++++++");
		ra.addAttribute("uname","张三");//设置传参避免乱码
		return "redirect:action2.do";
	}
	@RequestMapping("/action2")
	public String action2(String uname){
		System.out.println("+++++++++++++++执行action2方法+++++++++++++++++++++++++++");
		System.out.println(uname);
		return "success";
		}
	}
